package cm.tl.pms.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;

@Slf4j
public class CustomJsonUtil {
    public static JSONObject parseObject(String jsonStr) {
        JSONObject jo = new JSONObject();
        try {
            jsonStr = preDealStr(jsonStr);
            System.out.println(jsonStr);
            while (jsonStr.length() > 0) {
                jsonStr = parseLastKV(jsonStr, jo);

            }
        } catch (Exception e) {
            log.error("这个json串太离谱,真的解析不了...");
        }
        return jo;
    }

    private static String parseLastKV(String jsonStr, JSONObject jo) {
        int lastValStart = jsonStr.lastIndexOf(":");
        if (lastValStart == -1 || lastValStart == 0) {
            // 到头了,丢弃
            return "";
        }
        String lastVal = jsonStr.substring(lastValStart + 1);
        jsonStr = jsonStr.substring(0, lastValStart);
        int lastKeyStart = jsonStr.lastIndexOf(",");
        String lastKey;
        if (lastKeyStart == -1 || lastKeyStart == 0) {
            // 到头了,丢弃
            lastKey = jsonStr;
            jsonStr = "";
        } else {
            lastKey = jsonStr.substring(lastKeyStart + 1);
            jsonStr = jsonStr.substring(0, lastKeyStart);
        }
        lastKey = fmtKV(lastKey);
        lastVal = fmtKV(lastVal);
        jo.put(lastKey, lastVal);
        System.out.println("===========================================================");
        System.out.println("" + lastKey + ":" + lastVal);
        System.out.println(jsonStr);
        return jsonStr;
    }

    private static String fmtKV(String str) {
        str = str.trim();
        if (str.startsWith("\"")) {
            str = str.substring(1);
        }
        if (str.endsWith("\"")) {
            str = str.substring(0, str.length() - 1);
        }
        str = str.replaceAll(",", "，");
        return str;
    }

    private static String preDealStr(String jsonStr) {
        jsonStr = jsonStr.replaceAll("\n", "");
        jsonStr = jsonStr.replaceAll("，", ",");
        jsonStr = jsonStr.replaceAll(" *\"", "\"");
        jsonStr = jsonStr.replaceAll("\" *", "\"");
        if (jsonStr.startsWith("{")) {
            jsonStr = jsonStr.substring(1);
        }
        if (jsonStr.endsWith("}")) {
            jsonStr = jsonStr.substring(0, jsonStr.length() - 1);
        }
        return jsonStr;
    }

    public static String parseAiResult(String result) {
        if (StringUtils.isBlank(result)) {
            return "";
        }
        JSONObject jo;
        try {
            jo = JSON.parseObject(result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            jo = CustomJsonUtil.parseObject(result);
        }
        Iterator<Object> it = jo.values().iterator();
        while (it.hasNext()) {
            return it.next().toString();
        }
        return result;
    }

    public static void main(String[] args) {
        String s = "{\n" + "    \"诉讼记录\": 未提及具体信息，故无法判断，\n" + "    \"客户贷款不良信贷记录\": 未提及具体信息，故无法判断，\n" + "    " +
                "\"垫款记录\":" + " 未提及具体信息，故无法判断，\n" + "    \"行政处罚记录\": 未提及具体信息，故无法判断，\n" + "    \"客户信誉情况\": \"未知\"，\n" + "    " + "\"欠息记录\": 未提及具体信息，故无法判断\n" + "}";
        parseObject(s);
    }
}
